﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace UDS.Components
{
    /// <summary>
    /// 文档类
    /// </summary>
    public class DocumentClass
    {
        #region 获取某项目中的文档 返回DataTable

        /// <summary>
        ///获取某项目中的文档 返回DataTable
        /// </summary>
        /// <param name="ClassID">项目ID</param>
        public DataTable GetClassDocs(int ClassID)
        {
            var data = new Database();
            SqlParameter[] parameters =
                {
                    Database.MakeInParam("@ClassID", SqlDbType.Int, 20, ClassID)
                };

            try
            {
                using (SqlDataReader dataReader = data.RunDataReader("dbo.sproc_GetNewDocument", parameters))
                {
                    return Tools.ConvertDataReaderToDataTable(dataReader);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                return null;
            }
        }

        #endregion

        #region 获取某项目我的审批文档 返回DataTable

        /// <summary>
        /// 获取某项目我的审批文档 返回DataTable 返回DataTable
        /// </summary>
        /// <param name="ClassID">项目ID</param>
        public DataTable GetApproveClassDocs(int ClassID)
        {
            var data = new Database();
            SqlParameter[] parameters =
                {
                    Database.MakeInParam("@ClassID", SqlDbType.Int, 20, ClassID)
                };

            try
            {
                using (SqlDataReader dataReader = data.RunDataReader("dbo.sproc_GetMyClassApproved", parameters))
                {
                    return Tools.ConvertDataReaderToDataTable(dataReader);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                return null;
            }
        }

        #endregion

        #region 获取某文档详细信息

        /// <summary>
        /// 获取某文档详细信息
        /// </summary>
        public SqlDataReader GetDocDetail(int DocID, string UserName)
        {
            var data = new Database();
            SqlParameter[] parameters =
                {
                    Database.MakeInParam("@Doc_ID", SqlDbType.Int, 20, DocID),
                    Database.MakeInParam("@UserName", SqlDbType.NVarChar, 300, UserName)
                };
            try
            {
                return data.RunDataReader("dbo.sproc_ReadDocument", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                return null;
            }
        }

        #endregion

        #region 文件主体添加

        /// <summary>
        /// 文件主体添加
        /// </summary>
        /// <param name="docbody"> 文件主体类</param>
        public string AddDocBody(DocBody docbody)
        {
            // create data object and params
            var data = new Database();
            string DocID;
            var parameters = new[]
            {
                Database.MakeInParam("@ClassID", SqlDbType.Int, 20, docbody.DocClassID),
                Database.MakeInParam("@DocTitle", SqlDbType.NVarChar, 300, docbody.DocTitle),
                Database.MakeInParam("@DocContent", SqlDbType.NVarChar, 5000, docbody.DocContent),
                Database.MakeInParam("@DocApprover", SqlDbType.NVarChar, 200, docbody.DocApprover),
                Database.MakeInParam("@DocApproveDate", SqlDbType.NVarChar, 20,
                    docbody.DocApproveDate),
                Database.MakeInParam("@DocApproved", SqlDbType.Int, 1, docbody.DocApproved),
                Database.MakeInParam("@DocType", SqlDbType.Int, 2, docbody.DocType),
                Database.MakeInParam("@DocAttribute", SqlDbType.Int, 2, docbody.DocAttribute),
                Database.MakeInParam("@DocAddedBy", SqlDbType.NVarChar, 300, docbody.DocAddedBy),
                Database.MakeInParam("@DocAddedDate", SqlDbType.DateTime, 300,
                    DateTime.Parse(docbody.DocAddedDate)),
                Database.MakeOutParam("@CurrentDocID", SqlDbType.Int, 2)
            };


            try
            {
                data.RunProc("dbo.sproc_Ext_AddDocument", parameters);
                DocID = parameters[10].Value.ToString();
                if (DocID == string.Empty)
                {
                    return null;
                }
                return DocID;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("文档正文增加错误!", ex);
            }
        }

        #endregion

        #region 文件附件数据库操作

        /// <summary>
        /// 文件附件数据库操作
        /// </summary>
        /// <param name="att">DocAttachFile类</param>
        /// <param name="DocID">文件ID</param>
        public void AddAttach(DocAttachFile att, int DocID)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@DocID", SqlDbType.Int, 20, DocID),
                Database.MakeInParam("@FileName", SqlDbType.NVarChar, 300, att.FileName),
                Database.MakeInParam("@FileSize", SqlDbType.Int, 20, att.FileSize),
                Database.MakeInParam("@FileAttribute", SqlDbType.SmallInt, 20, att.FileAttribute),
                Database.MakeInParam("@FileVisualPath", SqlDbType.NVarChar, 200, att.FileVisualPath),
                Database.MakeInParam("@FileAuthor", SqlDbType.NVarChar, 50, att.FileAuthor),
                Database.MakeInParam("@FileCatlog", SqlDbType.NVarChar, 20, att.FileCatlog),
                Database.MakeInParam("@FileAddedDate", SqlDbType.DateTime, 30,
                    DateTime.Parse(att.FileAddedDate))
            };
            try
            {
                data.RunProc("dbo.sproc_Ext_AddFile", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("文件附件发送出错!", ex);
            }
        }

        #endregion

        #region 获取某项目中的文档列表

        /// <summary>
        /// 获取某项目中的文档列表
        /// </summary>
        /// <param name="ClassID">项目ID</param>
        /// <param name="UserName">用户名D</param>
        /// <param name="DisplayType">The display type.</param>
        public SqlDataReader GetDocListInClass(int ClassID, string UserName, int DisplayType)
        {
            var data = new Database();
            SqlParameter[] parameters =
                {
                    Database.MakeInParam("@ClassID", SqlDbType.Int, 20, ClassID),
                    Database.MakeInParam("@UserName", SqlDbType.NVarChar, 20, UserName),
                    Database.MakeInParam("@DisplayType", SqlDbType.Int, 1, DisplayType)
                };
            try
            {
                return data.RunDataReader("dbo.sproc_GetClassDocumentList", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                return null;
            }
        }

        #endregion

        #region 删除一组文档

        /// <summary>
        /// 删除一组文档
        /// </summary>
        /// <param name="DocIDStr">文档ID的连接字符串，用逗号相隔开</param>
        /// <param name="DeleteType">The type of the deletion action.</param>
        public bool DocDelete(string DocIDStr, int DeleteType)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@DocIDs", SqlDbType.NVarChar, 4000, DocIDStr),
                Database.MakeInParam("@DeleteType", SqlDbType.Int, 1, DeleteType)
            };
            try
            {
                data.RunProc("dbo.sproc_DeleteDocument", parameters);
                return true;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                return false;
            }
        }

        #endregion

        #region 获取管理者查询快捷文档信息

        /// <summary>
        /// 获取某文档路径信息
        /// </summary>
        public SqlDataReader GetManageQueryDetail()
        {
            var data = new Database();
            try
            {
                return data.RunDataReader("dbo.sproc_GetManagerDocument");
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                return null;
            }
        }

        #endregion
    }
}